Life Expectancy Prediction using Linear Regression

In [2]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
import plotly.express as px
from sklearn.model_selection import train_test_split
In [3]:
df=pd.read_csv('Life expectancy data.csv')
df.head()
Out[3]:
Country Year Status Life expectancy Adult Mortality infant deaths Alcohol percentage expenditure Hepatitis B Measles ... Polio Total expenditure Diphtheria HIV/AIDS GDP Population thinness 1-19 years thinness 5-9 years Income composition of resources Schooling
0 Afghanistan 2015 Developing 65.0 263.0 62 0.01 71.279624 65.0 1154 ... 6.0 8.16 65.0 0.1 584.259210 33736494.0 17.2 17.3 0.479 10.1
1 Afghanistan 2014 Developing 59.9 271.0 64 0.01 73.523582 62.0 492 ... 58.0 8.18 62.0 0.1 612.696514 327582.0 17.5 17.5 0.476 10.0
2 Afghanistan 2013 Developing 59.9 268.0 66 0.01 73.219243 64.0 430 ... 62.0 8.13 64.0 0.1 631.744976 31731688.0 17.7 17.7 0.470 9.9
3 Afghanistan 2012 Developing 59.5 272.0 69 0.01 78.184215 67.0 2787 ... 67.0 8.52 67.0 0.1 669.959000 3696958.0 17.9 18.0 0.463 9.8
4 Afghanistan 2011 Developing 59.2 275.0 71 0.01 7.097109 68.0 3013 ... 68.0 7.87 68.0 0.1 63.537231 2978599.0 18.2 18.2 0.454 9.5

5 rows × 22 columns

In [4]:
df.shape
Out[4]:
(2938, 22)
In [5]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2938 entries, 0 to 2937
Data columns (total 22 columns):
 #   Column                           Non-Null Count  Dtype  
---  ------                           --------------  -----  
 0   Country                          2938 non-null   object 
 1   Year                             2938 non-null   int64  
 2   Status                           2938 non-null   object 
 3   Life expectancy                  2928 non-null   float64
 4   Adult Mortality                  2928 non-null   float64
 5   infant deaths                    2938 non-null   int64  
 6   Alcohol                          2744 non-null   float64
 7   percentage expenditure           2938 non-null   float64
 8   Hepatitis B                      2385 non-null   float64
 9   Measles                          2938 non-null   int64  
 10   BMI                             2904 non-null   float64
 11  under-five deaths                2938 non-null   int64  
 12  Polio                            2919 non-null   float64
 13  Total expenditure                2712 non-null   float64
 14  Diphtheria                       2919 non-null   float64
 15   HIV/AIDS                        2938 non-null   float64
 16  GDP                              2490 non-null   float64
 17  Population                       2286 non-null   float64
 18   thinness  1-19 years            2904 non-null   float64
 19   thinness 5-9 years              2904 non-null   float64
 20  Income composition of resources  2771 non-null   float64
 21  Schooling                        2775 non-null   float64
dtypes: float64(16), int64(4), object(2)
memory usage: 505.1+ KB
In [6]:
df.isnull().sum()
Out[6]:
Country                              0
Year                                 0
Status                               0
Life expectancy                     10
Adult Mortality                     10
infant deaths                        0
Alcohol                            194
percentage expenditure               0
Hepatitis B                        553
Measles                              0
 BMI                                34
under-five deaths                    0
Polio                               19
Total expenditure                  226
Diphtheria                          19
 HIV/AIDS                            0
GDP                                448
Population                         652
 thinness  1-19 years               34
 thinness 5-9 years                 34
Income composition of resources    167
Schooling                          163
dtype: int64
In [7]:
df.describe()
Out[7]:
Year Life expectancy Adult Mortality infant deaths Alcohol percentage expenditure Hepatitis B Measles BMI under-five deaths Polio Total expenditure Diphtheria HIV/AIDS GDP Population thinness 1-19 years thinness 5-9 years Income composition of resources Schooling
count 2938.000000 2928.000000 2928.000000 2938.000000 2744.000000 2938.000000 2385.000000 2938.000000 2904.000000 2938.000000 2919.000000 2712.00000 2919.000000 2938.000000 2490.000000 2.286000e+03 2904.000000 2904.000000 2771.000000 2775.000000
mean 2007.518720 69.224932 164.796448 30.303948 4.602861 738.251295 80.940461 2419.592240 38.321247 42.035739 82.550188 5.93819 82.324084 1.742103 7483.158469 1.275338e+07 4.839704 4.870317 0.627551 11.992793
std 4.613841 9.523867 124.292079 117.926501 4.052413 1987.914858 25.070016 11467.272489 20.044034 160.445548 23.428046 2.49832 23.716912 5.077785 14270.169342 6.101210e+07 4.420195 4.508882 0.210904 3.358920
min 2000.000000 36.300000 1.000000 0.000000 0.010000 0.000000 1.000000 0.000000 1.000000 0.000000 3.000000 0.37000 2.000000 0.100000 1.681350 3.400000e+01 0.100000 0.100000 0.000000 0.000000
25% 2004.000000 63.100000 74.000000 0.000000 0.877500 4.685343 77.000000 0.000000 19.300000 0.000000 78.000000 4.26000 78.000000 0.100000 463.935626 1.957932e+05 1.600000 1.500000 0.493000 10.100000
50% 2008.000000 72.100000 144.000000 3.000000 3.755000 64.912906 92.000000 17.000000 43.500000 4.000000 93.000000 5.75500 93.000000 0.100000 1766.947595 1.386542e+06 3.300000 3.300000 0.677000 12.300000
75% 2012.000000 75.700000 228.000000 22.000000 7.702500 441.534144 97.000000 360.250000 56.200000 28.000000 97.000000 7.49250 97.000000 0.800000 5910.806335 7.420359e+06 7.200000 7.200000 0.779000 14.300000
max 2015.000000 89.000000 723.000000 1800.000000 17.870000 19479.911610 99.000000 212183.000000 87.300000 2500.000000 99.000000 17.60000 99.000000 50.600000 119172.741800 1.293859e+09 27.700000 28.600000 0.948000 20.700000
In [8]:
df.describe(include = object)
Out[8]:
Country Status
count 2938 2938
unique 193 2
top Afghanistan Developing
freq 16 2426
In [9]:
df.columns
Out[9]:
Index(['Country', 'Year', 'Status', 'Life expectancy ', 'Adult Mortality',
       'infant deaths', 'Alcohol', 'percentage expenditure', 'Hepatitis B',
       'Measles ', ' BMI ', 'under-five deaths ', 'Polio', 'Total expenditure',
       'Diphtheria ', ' HIV/AIDS', 'GDP', 'Population',
       ' thinness  1-19 years', ' thinness 5-9 years',
       'Income composition of resources', 'Schooling'],
      dtype='object')
In [10]:
df.isnull().sum()
Out[10]:
Country                              0
Year                                 0
Status                               0
Life expectancy                     10
Adult Mortality                     10
infant deaths                        0
Alcohol                            194
percentage expenditure               0
Hepatitis B                        553
Measles                              0
 BMI                                34
under-five deaths                    0
Polio                               19
Total expenditure                  226
Diphtheria                          19
 HIV/AIDS                            0
GDP                                448
Population                         652
 thinness  1-19 years               34
 thinness 5-9 years                 34
Income composition of resources    167
Schooling                          163
dtype: int64
In [11]:
from sklearn.impute import SimpleImputer
imputer=SimpleImputer(missing_values=np.nan, strategy='mean', fill_value=None)
df['Life expectancy ']=imputer.fit_transform(df[['Life expectancy ']])
In [12]:
df.isnull().sum()
Out[12]:
Country                              0
Year                                 0
Status                               0
Life expectancy                      0
Adult Mortality                     10
infant deaths                        0
Alcohol                            194
percentage expenditure               0
Hepatitis B                        553
Measles                              0
 BMI                                34
under-five deaths                    0
Polio                               19
Total expenditure                  226
Diphtheria                          19
 HIV/AIDS                            0
GDP                                448
Population                         652
 thinness  1-19 years               34
 thinness 5-9 years                 34
Income composition of resources    167
Schooling                          163
dtype: int64
In [13]:
df['Adult Mortality']=imputer.fit_transform(df[['Adult Mortality']])
df['Alcohol']=imputer.fit_transform(df[['Alcohol']])
df['Hepatitis B']=imputer.fit_transform(df[['Hepatitis B']])
df[' BMI ']=imputer.fit_transform(df[[' BMI ']])
df['Polio']=imputer.fit_transform(df[['Polio']])
df['Total expenditure']=imputer.fit_transform(df[['Total expenditure']])
df['Diphtheria ']=imputer.fit_transform(df[['Diphtheria ']])
df['GDP']=imputer.fit_transform(df[['GDP']])
df['Population']=imputer.fit_transform(df[['Population']])
df[' thinness  1-19 years']=imputer.fit_transform(df[[' thinness  1-19 years']])
df[' thinness 5-9 years']=imputer.fit_transform(df[[' thinness 5-9 years']])
df['Schooling']=imputer.fit_transform(df[['Schooling']])
df['Income composition of resources']=imputer.fit_transform(df[['Income composition of resources']])
In [14]:
df.isnull().sum()
Out[14]:
Country                            0
Year                               0
Status                             0
Life expectancy                    0
Adult Mortality                    0
infant deaths                      0
Alcohol                            0
percentage expenditure             0
Hepatitis B                        0
Measles                            0
 BMI                               0
under-five deaths                  0
Polio                              0
Total expenditure                  0
Diphtheria                         0
 HIV/AIDS                          0
GDP                                0
Population                         0
 thinness  1-19 years              0
 thinness 5-9 years                0
Income composition of resources    0
Schooling                          0
dtype: int64
In [15]:
LR=LinearRegression()
In [16]:
#LR.fit('Status', 'life expectancy')
#Status=np.array(['Status']).reshape(-1,1)
#Life_expectancy=np.array(['Life expectancy'])
In [17]:
#life_expectancy=np.array(['Life expectancy']).reshape(-1,1)
#LR.fit(life_expectancy, Status)
In [18]:
target=df['Life expectancy ']
features=df[df.columns.difference(['Life expectancy ', 'Year'])]
In [19]:
x_train, x_test, y_train, y_test= train_test_split(pd.get_dummies(features), target, test_size=0.3)
In [20]:
LR.fit(x_train, y_train)
Out[20]:
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
In [21]:
LR.confidence=LR.score(x_test, y_test)
In [22]:
print('Confidence: ',LR.confidence)
Confidence:  0.9487860011999055
In [23]:
y_pred= LR.predict(x_test)
In [24]:
y_pred
Out[24]:
array([81.85419968, 67.92341237, 63.016682  , 50.36671115, 69.96883514,
       65.53567492, 56.30445063, 67.35053485, 75.09553312, 69.30914511,
       73.41097751, 75.18563566, 75.02683601, 74.6361224 , 67.80414756,
       68.67625386, 74.13642529, 64.93813022, 55.88557134, 74.579394  ,
       76.47216953, 69.80990543, 80.75913176, 69.37032588, 69.51204204,
       82.52777391, 65.38201439, 80.30732601, 76.77864358, 64.94018683,
       56.08991717, 78.52788793, 75.91809007, 74.33704424, 49.81369249,
       74.78614199, 72.81807758, 72.10426166, 74.13355367, 59.65229487,
       61.58828443, 81.55922399, 80.49697381, 58.0797026 , 65.77691081,
       78.35841205, 64.64583721, 58.44452776, 67.18145456, 45.7424849 ,
       81.37422286, 65.45648501, 56.98734108, 72.18397753, 44.54714595,
       63.09965117, 80.4701423 , 72.71065435, 81.8971594 , 65.11332245,
       80.31023132, 75.67317245, 73.9141879 , 76.78226308, 67.75360851,
       74.2518846 , 59.37104583, 70.04939339, 80.66506238, 60.28663969,
       61.06909435, 65.55927562, 71.72723521, 74.08612318, 82.21611116,
       81.67237996, 61.42536363, 75.38761468, 71.95253921, 81.27229826,
       65.85361574, 48.48133983, 56.45144782, 82.91437736, 45.39432981,
       47.09127591, 81.06100099, 68.52519408, 60.60609032, 73.75745595,
       53.91606876, 68.69493527, 73.95984356, 77.44985671, 54.28442908,
       66.62065872, 72.00078053, 76.65888394, 82.25505076, 77.1447203 ,
       52.61370758, 72.96889479, 77.21067276, 66.78107706, 79.50533325,
       56.23978691, 50.92940109, 73.31109466, 80.38333062, 73.81445277,
       76.81280842, 57.69792237, 69.74510664, 81.5985041 , 75.30017726,
       55.26352325, 46.74937256, 73.79181912, 75.05696551, 75.15693334,
       56.79151061, 82.76049632, 73.63678612, 81.76138833, 77.55015621,
       68.54102574, 79.92541759, 53.86760899, 75.31204515, 68.18377351,
       75.91167288, 56.91231934, 60.88255646, 74.03693308, 49.0499561 ,
       78.24317545, 68.84348473, 73.80662707, 81.89673489, 67.64431277,
       83.7983134 , 82.19019848, 66.82150506, 77.41999727, 57.10088739,
       57.35139543, 84.17805688, 71.89839534, 56.24871715, 83.28709155,
       60.44750604, 68.69747954, 80.632294  , 61.55065843, 54.53958577,
       73.36468225, 76.1361586 , 56.70264438, 82.72059229, 80.848434  ,
       60.82869099, 61.65747887, 53.49176909, 71.56224208, 68.05910228,
       76.39568686, 65.68860841, 69.55714579, 63.5019186 , 79.4298939 ,
       59.79062215, 64.87920472, 81.37124428, 81.77660038, 75.04907039,
       73.11809899, 64.50670083, 56.99779022, 74.82963878, 63.55181108,
       46.76522472, 79.94204306, 50.5461329 , 44.26761581, 67.83256361,
       72.29611315, 56.77999634, 68.46089021, 72.99933795, 74.66880817,
       64.19149049, 77.47280149, 78.16236842, 69.04631017, 68.49468137,
       82.02060136, 75.77160462, 76.18776684, 62.61430271, 65.55530529,
       61.4647328 , 72.10317149, 64.54384876, 53.7933153 , 74.05584908,
       73.77288514, 71.73329919, 56.13331662, 78.88859613, 42.28626892,
       76.26732817, 72.08136755, 73.18835686, 73.34008711, 73.31794355,
       69.65998289, 73.20576687, 69.24099083, 75.90463176, 59.07647481,
       81.86811685, 60.79873231, 61.21836873, 57.74974711, 75.02670276,
       82.27708243, 62.56292404, 57.4658889 , 74.11340186, 76.55753402,
       75.74177489, 74.52652377, 58.86693326, 65.60065365, 50.98757388,
       66.49127171, 75.51179387, 82.74375373, 71.99930819, 63.61686135,
       72.29107314, 82.41388756, 65.98656005, 81.25166877, 57.70111446,
       54.42440247, 67.96249626, 76.02166144, 63.6435276 , 77.7457721 ,
       50.56686819, 81.89038303, 56.90599822, 64.92612522, 72.93627543,
       63.33398641, 70.77730424, 72.75033533, 61.19622281, 81.28620337,
       78.96648772, 53.19982346, 55.27224783, 59.61576188, 69.08285996,
       62.81141687, 72.95477182, 72.15662777, 59.1269013 , 80.571666  ,
       79.66397571, 73.50674832, 68.24233969, 64.1328705 , 71.42653566,
       56.59690272, 73.1801266 , 52.4279138 , 70.69396063, 57.06868377,
       54.44730592, 58.41663702, 58.99580022, 79.33405405, 59.37791557,
       71.82146518, 56.23347431, 55.36568175, 73.13040001, 65.72948735,
       73.81727319, 65.52358885, 74.2396838 , 68.54724213, 64.65763836,
       80.56353593, 60.5643533 , 56.62984208, 72.86257107, 50.65069056,
       80.59650249, 82.74183119, 62.69437795, 68.54136163, 70.89187549,
       69.32182096, 72.25577713, 76.09186129, 69.33490045, 62.53154411,
       53.97574879, 52.68253978, 74.70238027, 58.99516341, 82.97092528,
       74.25921616, 54.52236889, 70.07881516, 83.30668737, 78.1701195 ,
       79.70345987, 55.96900471, 76.01236999, 81.35654551, 80.16150076,
       54.20135578, 50.03408356, 82.3300927 , 67.33957642, 73.90098989,
       71.38929046, 71.54063148, 76.65238957, 56.62564317, 82.02844175,
       62.9727764 , 54.11781478, 71.73116892, 82.64685695, 48.75450322,
       77.56542482, 83.10590218, 74.1226752 , 72.80030571, 55.71903353,
       79.60809297, 61.15900153, 72.67962465, 76.02538333, 73.88445881,
       83.08977062, 58.72228865, 73.79290753, 53.77000928, 68.39909436,
       75.28850758, 73.9892441 , 72.78290765, 52.73899705, 73.94521129,
       54.00082712, 81.27547271, 68.48858643, 71.11083954, 47.51847232,
       70.62430715, 72.91764631, 47.36027629, 70.0804118 , 64.99719561,
       58.75310041, 52.37532774, 70.39132274, 50.08004159, 73.1032761 ,
       78.1530071 , 55.86517055, 75.82868037, 83.53049964, 74.03234809,
       69.73964722, 70.94479547, 63.46890725, 51.89510925, 57.50154572,
       76.74110733, 81.54815829, 61.56021624, 68.04493027, 72.81973507,
       82.3165796 , 70.16614309, 67.9048524 , 64.30630549, 59.03572668,
       74.18350818, 71.51860355, 72.49267337, 73.61708695, 75.06986261,
       64.76207864, 73.79979301, 68.06157431, 56.52972823, 71.01172614,
       68.28643967, 74.23324707, 73.62644988, 64.83159229, 72.18546614,
       74.68870166, 62.5035707 , 75.2039345 , 72.34573016, 63.26299077,
       76.85523659, 64.9394638 , 73.1793442 , 62.92390484, 74.8045401 ,
       72.50767447, 58.46830423, 73.77794639, 75.62237313, 64.74667836,
       72.32720679, 73.75806663, 81.88488532, 73.78329638, 67.86011543,
       75.56389376, 74.56336801, 75.3731793 , 52.08344802, 73.61806669,
       62.75775846, 65.07520601, 73.31279105, 74.23926718, 50.10138437,
       62.22432424, 59.97215992, 49.59662849, 74.69745495, 73.83779736,
       75.99158431, 49.20832766, 70.9379247 , 73.04337219, 73.93514917,
       58.34396419, 75.99162499, 62.10871428, 65.52536772, 69.27315407,
       57.06393342, 73.79169748, 72.97319159, 81.95228006, 68.24296889,
       74.41026806, 80.46501607, 55.70291911, 79.39697817, 76.98110408,
       44.20746743, 64.48000891, 76.87586095, 58.99603852, 78.84411849,
       76.13803344, 75.10840248, 79.46687779, 63.40237107, 68.66088117,
       48.15297671, 74.15668307, 68.08364448, 73.48844601, 73.17807973,
       71.96587174, 71.55529544, 79.74994359, 56.47359485, 80.95693201,
       61.56292599, 72.25613778, 74.58337371, 64.48132545, 79.75538908,
       65.22412617, 69.23665804, 70.36491862, 68.70087457, 47.48339946,
       73.98543737, 75.39566405, 68.77795654, 56.29972251, 73.15780146,
       73.90030349, 65.71638898, 69.95892969, 80.52990076, 81.70973071,
       79.41215385, 64.01974133, 72.11468777, 82.72499   , 79.57302263,
       81.55955701, 60.56483741, 73.09228437, 69.81980483, 51.02737001,
       74.72378131, 62.52568566, 55.24174932, 58.20879511, 69.83742298,
       59.16966866, 75.94060864, 75.14754758, 62.96811216, 53.09790919,
       60.01328517, 56.24318241, 70.30795708, 75.25340316, 59.27652578,
       71.97178922, 57.06709409, 75.0481521 , 73.68643089, 70.10462424,
       56.35590004, 61.21045595, 72.99501193, 62.65613   , 55.64648397,
       81.03935948, 63.75287712, 70.08877802, 81.40897513, 74.61612175,
       73.22666547, 59.84775414, 74.03443585, 75.15499619, 59.57253862,
       55.18355814, 66.20254003, 62.07858684, 71.89150338, 72.85115103,
       80.72842808, 81.40022168, 75.43501936, 74.65728966, 54.79664538,
       75.11573865, 57.54183567, 70.60208447, 72.96918835, 64.7632742 ,
       75.28870033, 45.17601139, 73.23428341, 67.46114876, 80.24279611,
       72.93833673, 72.48731548, 79.22419628, 51.07781447, 58.77188772,
       61.28020751, 73.60224019, 66.62140416, 64.37096615, 78.8764211 ,
       63.27741778, 50.3306288 , 68.86160599, 69.20095966, 81.74873594,
       56.83054866, 66.4522241 , 63.55770765, 80.04124671, 77.3889089 ,
       49.127913  , 57.66403556, 75.63311503, 80.59971029, 74.41133254,
       83.20536732, 80.81122263, 76.4946158 , 65.45664665, 76.67322931,
       75.58998509, 54.18122289, 75.51111052, 74.13768523, 66.56802527,
       67.58000295, 66.75781485, 65.06403412, 80.47308509, 80.45306075,
       66.02707757, 68.05304203, 66.34534869, 83.13292859, 81.37487215,
       73.59221864, 78.01400827, 54.86004101, 74.11260975, 54.26461958,
       81.65727877, 79.52717668, 46.51044907, 76.58697523, 72.97877356,
       63.03789558, 53.23667888, 65.61179822, 73.47784727, 78.47163422,
       71.44334394, 72.76797398, 74.13175961, 81.3875667 , 72.6093583 ,
       61.03924157, 64.40310215, 49.1104199 , 82.29365229, 72.3919204 ,
       79.7033055 , 56.96312346, 76.23402707, 73.8669064 , 62.98469596,
       68.96169393, 74.04340451, 63.18938276, 61.58914377, 76.66825828,
       74.92667482, 73.26621163, 73.37029592, 73.67087293, 59.30538499,
       72.64205173, 76.22281256, 65.78585898, 73.80415515, 64.02493902,
       75.90528103, 65.64453941, 61.2160441 , 78.38687018, 76.6663046 ,
       68.1588133 , 76.17947329, 74.93739343, 73.5853881 , 51.82967004,
       76.0660838 , 71.72248006, 78.08204348, 58.72268104, 80.93702233,
       74.40321311, 81.13082867, 53.5915655 , 69.50039915, 52.25910719,
       58.84955868, 76.37629198, 73.26346266, 80.18805734, 73.21716033,
       53.48706328, 71.81404744, 61.84631723, 74.136736  , 72.93888676,
       52.45770421, 62.41304388, 71.07944639, 80.64539763, 82.05616553,
       73.4876298 , 72.76980558, 61.10755305, 53.23906084, 76.51125538,
       58.9585047 , 52.60530515, 61.10486981, 60.9527611 , 66.50220605,
       63.93823543, 75.29464295, 71.57244778, 68.88964249, 75.85045002,
       75.74124003, 55.46531964, 65.84106581, 68.28797221, 69.11719687,
       78.46007073, 81.6858121 , 70.44150394, 73.45838105, 55.76230684,
       48.65907507, 79.12389471, 74.19600681, 44.80376631, 41.95132313,
       81.59498737, 63.08061101, 81.70325632, 75.09573602, 79.42744504,
       72.41582504, 74.73365143, 74.24475209, 74.06029129, 51.16995367,
       79.14384828, 60.49898598, 73.25840028, 72.12413577, 75.46018367,
       72.48826586, 67.68117411, 69.70191414, 66.2370562 , 75.33878351,
       83.44379341, 82.22482017, 74.07588135, 49.6712839 , 63.35149717,
       68.21904595, 74.0159575 , 76.84812951, 66.38891911, 66.05796689,
       63.40062326, 55.10705454, 74.39657889, 70.12174767, 71.17193587,
       81.15037472, 72.93822001, 69.02866829, 57.54971249, 74.89578371,
       64.8469979 , 75.7586391 , 73.95431545, 71.89081959, 72.58413694,
       66.32027679, 68.67937323, 76.13660726, 71.94943166, 67.9564615 ,
       58.81121777, 58.43994387, 53.39764988, 81.89882897, 63.72323483,
       62.0339733 , 70.38056752, 62.9967007 , 73.14852833, 80.01262557,
       68.20959753, 74.97994052, 81.4076586 , 72.40918526, 54.29415232,
       68.01395055, 65.6184783 , 65.99242096, 82.65248583, 74.13302565,
       74.39672344, 73.34270754, 58.01172656, 62.59134463, 72.79020707,
       53.02829256, 82.10148407, 71.86542853, 72.84521625, 66.34476962,
       79.83108419, 73.902185  , 72.86035875, 74.9147477 , 82.63948259,
       64.36698264, 74.6456966 , 55.34093227, 81.92226502, 72.41771032,
       81.0969912 , 73.90426284, 74.32428032, 45.30237513, 65.5250354 ,
       72.20141846, 74.69143543, 73.57329408, 72.07738396, 73.51893182,
       77.12002212, 73.38629978, 74.91868581, 58.32021119, 69.84402571,
       53.04280895, 74.18902946, 75.23406832, 72.18702893, 58.24787026,
       78.17428717, 64.08016645, 77.56839848, 83.83823577, 75.71992736,
       60.93267154, 82.21474172, 73.36150672, 73.82804986, 82.45945119,
       73.87680044, 49.86864186, 60.3976354 , 69.32630821, 56.15812846,
       66.18100598, 50.44709395, 70.10544623, 74.4926474 , 80.51459112,
       72.22151917, 62.52987479, 71.05827794, 73.99941886, 58.95219672,
       71.35020772, 73.50818654, 57.2119284 , 82.24554347, 66.52433227,
       68.92024895, 69.76766776, 80.15425615, 58.29692786, 80.4052314 ,
       55.94296893, 74.93504   , 74.25073525, 59.29438411, 69.27737618,
       74.35034726, 56.35579992, 72.92373154, 61.23480985, 72.36966158,
       74.07871046, 73.88789523])
In [25]:
plt.scatter(y_test, y_pred)
Out[25]:
<matplotlib.collections.PathCollection at 0x1e5a9a4be00>
No description has been provided for this image
In [26]:
plt.plot(y_test, y_pred)
Out[26]:
[<matplotlib.lines.Line2D at 0x1e5a5e0e3f0>]
No description has been provided for this image
In [27]:
mean_abs_err=mean_absolute_error(y_test, y_pred)
print('Mean absolute error: ',mean_abs_err)
Mean absolute error:  1.4058633740699258
In [28]:
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
rmae = np.sqrt(mae)

print("Mean Absolute Error (MAE):", mae)
print("Mean Squared Error (MSE):", mse)
print("Root Mean Squared Error (RMSE):", rmse)
print("Root Mean ABSOLUTE Error (RMSE):", rmae)
Mean Absolute Error (MAE): 1.4058633740699258
Mean Squared Error (MSE): 4.523359023056919
Root Mean Squared Error (RMSE): 2.126818991606225
Root Mean ABSOLUTE Error (RMSE): 1.1856910955514197
In [29]:
fig = px.pie(df, names='Status')
fig
In [74]:
df.columns
Out[74]:
Index(['Country', 'Year', 'Status', 'Life expectancy ', 'Adult Mortality',
       'infant deaths', 'Alcohol', 'percentage expenditure', 'Hepatitis B',
       'Measles ', ' BMI ', 'under-five deaths ', 'Polio', 'Total expenditure',
       'Diphtheria ', ' HIV/AIDS', 'GDP', 'Population',
       ' thinness  1-19 years', ' thinness 5-9 years',
       'Income composition of resources', 'Schooling'],
      dtype='object')
In [78]:
plt.figure(figsize=(8,8))
sns.scatterplot(x=df['Adult Mortality'], y=df['Life expectancy '], hue=df['Alcohol'])
Out[78]:
<Axes: xlabel='Adult Mortality', ylabel='Life expectancy '>
No description has been provided for this image